package com.niit.MostPopularTypeDecadeStart1980;


import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import java.util.Iterator;
import java.util.TreeMap;

public class MPTDSReducer extends Reducer<Text, MPTDSBean, Text, MPTDSBean> {
    private TreeMap<MPTDSBean, Text> flowMap80 = new TreeMap<>();
    private TreeMap<MPTDSBean, Text> flowMap90 = new TreeMap<>();
    private TreeMap<MPTDSBean, Text> flowMap00 = new TreeMap<>();
    private TreeMap<MPTDSBean, Text> flowMap10 = new TreeMap<>();
    @Override
    protected void reduce(Text key, Iterable<MPTDSBean> values, Context context) throws IOException, InterruptedException {
        for (MPTDSBean value :
                values) {
            MPTDSBean flowBean = new MPTDSBean();
            flowBean.set(value.getRate(), value.getRank(), value.getTypes(), value.getRegions(), value.getTitle(),
                    value.getRelease_data(), value.getActor_count(), value.getVote_count(), value.getScore(), value.getActors());

            Text movie = new Text(key);


//             1 向treeMap集合中添加数据

            if(flowBean.getRelease_data().contains("80")) flowMap80.put(flowBean,movie);
            if(flowBean.getRelease_data().contains("90")) flowMap90.put(flowBean,movie);
            if(flowBean.getRelease_data().contains("00")) flowMap00.put(flowBean,movie);
            if(flowBean.getRelease_data().contains("10")) flowMap10.put(flowBean,movie);

            // 2 限制TreeMap数据量，超过10条就删除掉流量最小的一条数据
            if (flowMap80.size() > 10) {
                flowMap80.remove(flowMap80.lastKey());
            }
            if (flowMap90.size() > 10) {
                flowMap90.remove(flowMap90.lastKey());
            }
            if (flowMap00.size() > 10) {
                flowMap00.remove(flowMap00.lastKey());
            }
            if (flowMap10.size() > 10) {
                flowMap10.remove(flowMap10.lastKey());
            }
        }
    }

    @Override
    protected void cleanup(Context context) throws IOException, InterruptedException {
        // 3 遍历集合，输出数据
        Iterator<MPTDSBean> iterator80 = flowMap80.keySet().iterator();
        Iterator<MPTDSBean> iterator90 = flowMap90.keySet().iterator();
        Iterator<MPTDSBean> iterator00 = flowMap00.keySet().iterator();
        Iterator<MPTDSBean> iterator10 = flowMap10.keySet().iterator();

        while (iterator80.hasNext()) {
            MPTDSBean valueOut = iterator80.next();
            Text keyOut = flowMap80.get(valueOut);
            context.write(keyOut, valueOut);
        }
        while (iterator90.hasNext()) {
            MPTDSBean valueOut = iterator90.next();
            Text keyOut = flowMap90.get(valueOut);
            context.write(keyOut, valueOut);
        }
        while (iterator00.hasNext()) {
            MPTDSBean valueOut = iterator00.next();
            Text keyOut = flowMap00.get(valueOut);
            context.write(keyOut, valueOut);
        }
        while (iterator10.hasNext()) {
            MPTDSBean valueOut = iterator10.next();
            Text keyOut = flowMap10.get(valueOut);
            context.write(keyOut, valueOut);
        }

    }
}
